package com.components.project.ui.fragment

import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.components.headerrecycler.RefreshHeaderView
import com.components.project.MainActivity
import com.components.project.R
import com.components.project.adapter.NewsAdapter
import com.components.project.databean.NewsDataBean
import com.components.project.ui.base.ProjectBaseFragment
import com.scwang.smart.refresh.layout.SmartRefreshLayout

class ListInfoFragment : ProjectBaseFragment<MainActivity>() {

    private var mUserHeadImage =
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Fec%2Fea%2F0f%2Fecea0f0a327ee84a8447ecddd997f663.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653703822&t=4053bf253835744911a0346f8db14302"

    private lateinit var refreshHeaderView: RefreshHeaderView

    private var mDataLists: MutableList<NewsDataBean> = mutableListOf()
    private lateinit var mDataAdapter: NewsAdapter
    private var mDetailsUrlList: MutableList<String> = mutableListOf()

    private fun initRefresh(smartRefreshLayout: SmartRefreshLayout) {
        smartRefreshLayout.setOnRefreshListener {
            Toast.makeText(context, "刷新", Toast.LENGTH_SHORT).show()
            smartRefreshLayout.finishRefresh()
            initData()
            mDataAdapter.notifyDataSetChanged()
        }
        smartRefreshLayout.setOnLoadMoreListener {
            Toast.makeText(context, "加载", Toast.LENGTH_SHORT).show()
            smartRefreshLayout.finishLoadMore()
        }
    }

    private fun addDetailsUrl() {
        mDetailsUrlList.add("https://www.baidu.com")
        mDetailsUrlList.add("https://juejin.cn/")
        mDetailsUrlList.add("https://www.zhihu.com/?utm_source=gold_browser_extension")
        mDetailsUrlList.add("https://www.bilibili.com/?utm_source=gold_browser_extension")
        mDetailsUrlList.add("https://www.jd.com/?utm_source=gold_browser_extension")
        mDetailsUrlList.add("https://news.cctv.com/")
        mDetailsUrlList.add("https://news.qq.com/")
        mDetailsUrlList.add("https://news.163.com/")
        mDetailsUrlList.add("https://h5.cqliving.com/info/detail/94283923.html?cid=94283923&f=10&sp=source_share&cqxhlwdc=3f")
        mDetailsUrlList.add("https://www.sina.com.cn/")
        mDetailsUrlList.add("https://www.zhihu.com/question/434554470/answer/2248733664")
        mDetailsUrlList.add("https://www.zhihu.com/question/263728862/answer/2518417662")
        mDetailsUrlList.add("https://tieba.baidu.com/index.html")
        mDetailsUrlList.add("https://tieba.baidu.com/f?kw=bilibili")
        mDetailsUrlList.add("https://www.pianku.la/")
        mDetailsUrlList.add("https://www.qidian.com/")
        mDetailsUrlList.add("https://www.x23us.us/3_32775/")
    }

    override fun getLayoutId(): Int {
       return R.layout.fragment_list_info
    }

    override fun initView() {
        mDetailsUrlList.clear()
        addDetailsUrl()
        mDataLists.clear()
        for (index in 0..16) {
            mDataLists.add(
                NewsDataBean(
                    "$index .新闻标题",
                    "浏览量：1000",
                    "2022-4-22",
                    mUserHeadImage,
                    mDetailsUrlList[index]
                )
            )
        }

        refreshHeaderView = findViewById(R.id.refreshHeaderView)

        mDataAdapter = NewsAdapter(ActivityUtils.getTopActivity(), mDataLists)
        refreshHeaderView.recyclerView.layoutManager = LinearLayoutManager(context)
        refreshHeaderView.recyclerView.adapter = mDataAdapter
        initRefresh(refreshHeaderView.smartRefreshLayout)
    }

    override fun initData() {
    }

}